<?php
class UsersController extends AppController {
    var $name = 'Users';
    
    function beforeFilter() {
	    $this->Auth->allow('login', 'add', 'view', 'index');
	}
	
	function index() {
	    $this->pageTitle = 'All Users';
	    $this->set('users', $this->User->find('all'));
	}
	
    function add() {
        $this->pageTitle = 'Join Us';
        if ($this->data) {
            if ($this->data['User']['password'] == $this->Auth->password($this->data['User']['password_confirm'])) {
                $this->User->create();
                $this->User->save($this->data);
                $this->redirect(array('action' => 'login'));
            }
            else {
                $this->Session->setFlash("Password doesn't match.");
            }
        }
    }
    
    function login() {
        $this->pageTitle = 'User Log in';
    }
    
    function logout() {
        $this->Session->setFlash('Thanks for using DarkBook Personal Library.');
        $this->redirect(array('action' => 'login'));
    }
    
    function home() {
        $this->pageTitle = 'My Library';
        $this->set('data', $this->User->findByUsername($this->Auth->user('username')));
    }
    
    function view($username = null) {
        $viewing = $this->User->findByUsername($username);
        $titleArray = array($viewing['User']['nickname'], "Library");
        $this->pageTitle = implode("'s ", $titleArray);
        $this->set('data', $viewing);
    }
    
}
?>
